// Copyright (C) 2014  by Achrouf Abdeour <achroufabdenour@gmail.com>
// Auteur Achrouf Abdenour

// This File is part of SmartCode

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.

#include "makefilegenerator.h"

#include <compiler/compilersetting.h>
#include <project.h>
#include <bases.h>
#include <buildvariables.h>

#include "core_constants.h"
#include "compiler/compiler.h"

MakeFileGenerator::MakeFileGenerator(QObject *parent):
  QObject(parent)
{

}

QString MakeFileGenerator::language()
{
  if(project->string(Core::Project::LANGUAGE) == "CPP")
    return Core::Compiler::CXX;
  else
    return Core::Compiler::CC;
}

void MakeFileGenerator::setSetting(CompilerSetting * s)
{
  setting = s;
}

void MakeFileGenerator::setProject(Project * pro)
{
  project = pro;

  globalVars->addVariable("PROJECTNAME",project->baseName());
  globalVars->addVariable("PROJECTDIR",project->path());

  Config * config = project->currentConfiguration();
  globalVars->addVariable("CONFIG",config->name());

  QFileInfo info(project->path());
  QString projectExecutable = info.baseName() + ".exe";
  globalVars->addVariable("DESTDIR",config->name());
  globalVars->addVariable("TARGET",projectExecutable);
  globalVars->addVariable("DESTDIR_TARGET",projectExecutable);
  globalVars->addVariable("OBJECTS_DIR","obj");
}

void MakeFileGenerator::setGlobalVariables(BuildVariables *globalVariables)
{
  globalVars = globalVariables;
}

void MakeFileGenerator::generateMakeFile()
{
  QString out;

  out +="#Makefile generated by SmartCode\n#Copyright (C) 2015 Achrouf corporation\n";

  out += Core::Compiler::CC ;
  out += "=" ;
  out +=setting->compiler()->CC() + "\n";
  out += Core::Compiler::CXX;
  out += "=" ;
  out += setting->compiler()->CXX()+ "\n";

  foreach(QString var, globalVars->projectVariables())
    {
      out += var;
      out += "= ";
      out += globalVars->value(var);
      out += "\n";
    }

  out+="CCFLAGS= -g -Wall\n";
  out+="LDFLAGS=\n";
  out+="OBJ=";

  foreach(QString s,project->stringList(Core::Project::SOURCES))
    {
      QString obj = s.replace(QFileInfo(s).suffix() , "o");

      out += " " ;

//      if(!project->currentConfiguration()->string(Core::Project::OBJECTSDIR).isEmpty())
//        out += "$(OBJECTS_DIR)/";

      out+= obj;
    }

  out += "\n";
  out += "INCS= ";

  foreach(QString inc,setting->stringList(Core::Compiler::CXXINC))
    {
      out += "-I\"" + inc+"\" "  ;
    }

  foreach(QString include,project->currentConfiguration()->stringList(Core::Project::INCLUDES))
    {
      out+="-I\"" + include+"\" "  ;
    }

  out+="\n";
  out+="LIBS= ";

  foreach(QString lib,setting->stringList(Core::Project::LIBS))
    {
      out+="-L\"" + lib+"\" "  ;
    }

  foreach(QString inclib,project->currentConfiguration()->stringList(Core::Project::INCLUDELIBS))
    {
      out+="-L\"" + inclib+"\" "  ;
    }

  foreach(QString lib,project->currentConfiguration()->stringList(Core::Project::LIBS))
    {
      out+=insertLib(lib) +" ";
    }

  out+="\n";
  out+="CXXFLAGS=$(INCS) -g -Wall";

  out+="\n";

  out+="all: $(DESTDIR_TARGET)\n";
  out+="$(DESTDIR_TARGET): $(OBJ)\n";

  switch(project->currentConfiguration()->toInt(Core::Project::TEMPLATE))
    {
    case Config::App:
      out+="\t$("+language()+") -o ";
      out+="$(DESTDIR_TARGET) $(OBJ) $(LIBS)\n";
      break;
    case Config::Lib:
      out+="\tar r $(DESTDIR_TARGET) $(OBJ)\n\tranlib $(DESTDIR_TARGET)";
      break;
    case Config::SharedLib:
      out+="\t$("+language()+") -shared -o ";
      out+="$(DESTDIR_TARGET) $(OBJ) $(LIBS)\n";
      break;
    }

  out +="\n";

  foreach(QString s,project->stringList(Core::Project::SOURCES))
    {
      QString name=s;
      QString obj=s.replace(QFileInfo(s).suffix(),"o");

      QString com="\t$("+language()+")";
      QString arg="$("+language()+"FLAGS)";

      out += obj + ": " + name;
      out += "\n";
      out += com + " -c " + name + " -o " + obj + " " + arg;
      out += "\n";
    }

  out +=".PHONY: clean mrproper\nclean:\n\tdel -rf $(OBJ)\n mrproper: clean\n\tdel -rf $(DESTDIR_TARGET)\n";

  Bases::out(project->makeFile() ,out);
}

QString MakeFileGenerator::insertLib(const QString &lib)
{
  if(QFileInfo(lib).baseName().startsWith("lib"))
    return lib;
  else
    return "-l"+lib;
}
